<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Html
 *
 * @author root
 */
class Html
{

    public static function heading($data, $h = 1, $attributes)
    {
        $attributes = ($attributes != '') ? ' ' . $attributes : $attributes;
        return "<h" . $h . $attributes . ">" . $data . "</h" . $h . ">";

    }

    public static function ul($list, $attributes = '')
    {
        return self::_list('ul', $list, $attributes);

    }

    public static function ol($list, $attributes = '')
    {
        return self::_list('ol', $list, $attributes);

    }

    public static function _list($type = 'ul', $list, $attributes = '', $depth = 0)
    {
        // If an array wasn't submitted there's nothing to do...
        if (!is_array($list))
        {
            return $list;
        }

        // Set the indentation based on the depth
        $out = str_repeat(" ", $depth);

        // Were any attributes submitted?  If so generate a string
        if (is_array($attributes))
        {
            $atts = '';
            foreach ($attributes as $key => $val)
            {
                $atts .= ' ' . $key . '="' . $val . '"';
            }
            $attributes = $atts;
        } elseif (is_string($attributes) AND strlen($attributes) > 0)
        {
            $attributes = ' ' . $attributes;
        }

        // Write the opening list tag
        $out .= "<" . $type . $attributes . ">\n";

        // Cycle through the list elements.  If an array is
        // encountered we will recursively call _list()

        static $_last_list_item = '';
        foreach ($list as $key => $val)
        {
            $_last_list_item = $key;

            $out .= str_repeat(" ", $depth + 2);
            $out .= "<li>";

            if (!is_array($val))
            {
                $out .= $val;
            } else
            {
                $out .= $_last_list_item . "\n";
                $out .= self::_list($type, $val, '', $depth + 4);
                $out .= str_repeat(" ", $depth + 2);
            }

            $out .= "</li>\n";
        }

        // Set the indentation for the closing tag
        $out .= str_repeat(" ", $depth);

        // Write the closing list tag
        $out .= "</" . $type . ">\n";

        return $out;

    }

    public static function br($num = 1)
    {
        return str_repeat("<br />", $num);

    }

    function img($src = '')
    {
        if (!is_array($src))
        {
            $src = array('src' => $src);
        }

        // If there is no alt attribute defined, set it to an empty string
        if (!isset($src['alt']))
        {
            $src['alt'] = '';
        }

        $img = '<img';

        foreach ($src as $k => $v)
        {

            $img .= " $k=\"$v\"";
        }

        $img .= '/>';

        return $img;

    }

    public static function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '')
    {

        $link = '<link ';

        if (is_array($href))
        {
            foreach ($href as $k => $v)
            {

                $link .= "$k=\"$v\" ";
            }

            $link .= "/>";
        } else
        {
            $link .= 'href="' . $href . '" ';

            $link .= 'rel="' . $rel . '" type="' . $type . '" ';

            if ($media != '')
            {
                $link .= 'media="' . $media . '" ';
            }

            if ($title != '')
            {
                $link .= 'title="' . $title . '" ';
            }

            $link .= '/>';
        }


        return $link;

    }

    public static function meta($name = '', $content = '', $type = 'name', $newline = "\n")
    {
        // Since we allow the data to be passes as a string, a simple array
        // or a multidimensional one, we need to do a little prepping.
        if (!is_array($name))
        {
            $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
        } else
        {
            // Turn single array into multidimensional
            if (isset($name['name']))
            {
                $name = array($name);
            }
        }

        $str = '';
        foreach ($name as $meta)
        {
            $type = (!isset($meta['type']) OR $meta['type'] == 'name') ? 'name' : 'http-equiv';
            $name = (!isset($meta['name'])) ? '' : $meta['name'];
            $content = (!isset($meta['content'])) ? '' : $meta['content'];
            $newline = (!isset($meta['newline'])) ? "\n" : $meta['newline'];

            $str .= '<meta ' . $type . '="' . $name . '" content="' . $content . '" />' . $newline;
        }

        return $str;

    }

}

